<template>
    <div>
        <!--权限组- component -->
        <div class="permissManaTool">
            <el-input placeholder="请输入角色英文名" v-model="role.name" size="small">
                <template slot="prepend">ROLE_</template>
            </el-input>
            <el-input placeholder="请输入角色中文名" v-model="role.nameZh" size="small"></el-input>
            <el-button type="primary" icon="el-icon-plus" @click="addRole">添加角色</el-button>
        </div>
        <div>
            <!-- el-collapse下拉菜单 -->
            <el-collapse accordion @change="change" v-model="activeName" style="width: 70%">
                <el-collapse-item :title="role.id+ ' ' + role.nameZh +'<'+role.name+'>'" :name="role.id"
                                    v-for="(role,index) in roles" :key="index">
                    <el-card class="box-card">
                        <div slot="header" class="clearfix">
                            <span>可访问资源</span>
                            <el-button style="float: right;padding: 4px 0 ;color: red" type="text"
                                        icon="el-icon-delete" @click="deleteRole(role)">删除角色
                            </el-button>
                        </div>
                        <div>
                            <!-- 树形控件 -->
                            <el-tree :data="allMenus" :props="defaultProps" show-checkbox
                                        :default-checked-keys="selectedMenus"
                                        node-key="id" ref="treeRef">
                            </el-tree>
                            <!-- 默认展开 -->
                            <div style="display: flex;justify-content: fiex-end">
                                <el-button type="primary" size="mini" @click="cancelUpdate">取消修改</el-button>
                                <el-button type="warning" size="mini" @click="doUpdate(role.id,index)">确认修改</el-button>
                            </div>
                        </div>
                    </el-card>
                </el-collapse-item>
            </el-collapse>
        </div>
    </div>
</template>

<script>
export default {
    name:"PermissMana",
    data(){
        return{
            // 添加消息
            role:{
                name:'',
                nameZh:'',
            },
            // 所有角色
            roles:[],
            // el-collspase下拉菜单
            allMenus:[],
            // 下拉菜单默认展开哪条， -1 不展开
            activeName: -1,
            // 树形控件显示数据
            defaultProps:{
                children: 'children',
                label: 'name',
            },
            // 树形控件默认选中
            selectedMenus:[],
        }
    },
    methods:{
        // 获取所有角色信息
        initRoles() {
            this.getRequest('/system/basic/permissions/').then(resp => {
                if (resp){
                    this.roles = resp;
                }
            })
        },
        // 获取所有菜单数据
        initAllMenus(){
            this.getRequest('/system/basic/permissions/menus').then(resp => {
                if (resp){
                    this.allMenus = resp;
                }
            })
        },
        // el-collapse下拉菜单展开/关闭事件，roleId就是role.id(选择展开哪个就传哪个哪个角色ID)
        change(roleId){
            if (roleId){
                // 清空树形控件默认选中，否则可能显示上次的数据，刷新不及时
                this.selectedMenus = [];
                // 获取所有菜单
                this.initAllMenus();
                // 获取角色拥有的菜单(打勾的)
                this.initSelectedMenus(roleId);
            }
        },
        // 获取角色拥有的菜单的ID，通过id选择选定(打勾)
        initSelectedMenus(roleId){
            this.getRequest('system/basic/permissions/mid/' + roleId).then(resp => {
                if (resp){
                    this.selectedMenus = resp;
                }
            })
        },
        // 确认修改角色所拥有的菜单
        doUpdate(roleId,index){
            // 通过ref拿到树形控件，通过index拿到当前操作的树形控件
            let tree = this.$refs.treeRef[index];
            // 通过树形控件的getCheckedKeys拿到当前选中得到菜单，设置true拿到被选中的子节点
            let selectedKeys = tree.getCheckedKeys(true);
            // 设置修改菜单请求路径
            let url = '/system/basic/permissions/?rid=' + roleId;
            // 遍历所有被选中的子菜单id，拼接到URL中
            selectedKeys.forEach(key => {
                url += '&mids=' + key;
            })
            // 修改请求
            this.postRequest(url).then(resp => {
                if (resp){
                    // 关闭展开
                    this.activeName = -1;
                }
            })
        },
        // 取消修改
        cancelUpdate(){
            this.activeName = -1;
        },
        // 添加角色
        addRole(){
            if (this.role.name && this.role.nameZh) {
                this.postRequest('/system/basic/permissions/role',this.role).then(resp =>{
                    console.log(this.role);
                    console.log(resp);
                    if (resp){
                        // 添加完成后重新获取所有角色数据
                        this.initRoles();
                        // 清空输入框
                        this.role.name = '';
                        this.role.nameZh = '';
                    }
                })
            }else {
                this.$message.error("添加的信息不能为空！");
            }
        },
        //删除角色
        deleteRole(role) {
            // 提示框
            this.$confirm('此操作将永久删除角色['+role.name+']，是否继续？','提示',{
                confirmButtonText:'确定',
                cancelButtonText:'取消',
                type :'warning'
            }).then(() => {
                this.deleteRequest('/system/basic/permissions/role/'+role.id).then(resp =>{
                    if (resp){
                        this.initRoles();
                    }
                })
            }).catch(() => {
                this.$message({
                    type: "info",
                    message: "已取消操作"
                })
            })
        }
    },
    mounted(){
        this.initRoles();
    }
}
</script>

<style>
    .permissManaTool {
        display: flex;
        justify-content: flex-start;
        margin-bottom: 10px;
    }

    .permissManaTool .el-input {
        width: 200px;
        margin-right: 8px;
    }
</style>